package com.ysd.lis.service.bac.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.bac.BacObserResult;
import com.ysd.lis.entity.bac.BacStainingResult;
import com.ysd.lis.entity.bac.BacStrain;
import com.ysd.lis.entity.bac.BacTestResult;
import com.ysd.lis.entity.inv.InvManufacturer;
import com.ysd.lis.entity.inv.InvReagent;
import com.ysd.lis.entity.inv.InvReagentStock;
import com.ysd.lis.entity.inv.InvStoreManage;
import com.ysd.lis.entity.lab.LabCriticalReport;
import com.ysd.lis.mapper.bac.BacObserResultMapper;
import com.ysd.lis.mapper.bac.BacStainingResultMapper;
import com.ysd.lis.mapper.bac.BacStrainMapper;
import com.ysd.lis.request.Params;
import com.ysd.lis.request.bac.BacBatchStrainVo;
import com.ysd.lis.service.bac.BacObserResultService;
import com.ysd.lis.service.bac.BacStrainService;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wanghuaixing
 * @since 2024-04-09
 */
@Service
public class BacStrainServiceImpl extends ServiceImpl<BacStrainMapper, BacStrain> implements BacStrainService {

    @Autowired
    BacStrainMapper bacStrainMapper;
    @Autowired
    BacObserResultMapper bacObserResultMapper;
    @Autowired
    BacObserResultService bacObserResultService;
    @Autowired
    BacStainingResultMapper bacStainingResultMapper;

    @Override
    public Result addStrain(BacStrain bacStrain) {
        boolean i = this.saveOrUpdate(bacStrain);
        if (i) {
            return Result.succ(1, "新增成功", "");
        } else {
            return Result.fail("添加菌株失败");
        }
    }

    @Override
    public Result addBatchStrain(BacBatchStrainVo bacBatchStrainVo) {
        List<BacStrain> bacStrainList = bacBatchStrainVo.getBacStrainList();
        this.saveOrUpdateBatch(bacStrainList);
        List<BacObserResult> obserResults = bacBatchStrainVo.getObserResults();
        bacObserResultService.saveBatch(obserResults);
        return Result.succ(1, "成功", "");
    }

    @Override
    public Result findStrain(Params params) {

        MPJLambdaWrapper<BacStrain> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(BacStrain.class);
        queryWrapper.selectAs( BacObserResult::getStrainCount, BacStrain::getStrainCount);
        queryWrapper.leftJoin(BacObserResult.class, p->p.eq(BacObserResult::getSampleda, BacStrain::getSampleda).eq(BacObserResult::getSampleNo, BacStrain::getSampleNo).eq(BacObserResult::getIsolateNum, BacStrain::getStrainCode).eq(BacObserResult::getRptGroup, BacStrain::getRptGroup));

        //LambdaQueryWrapper<BacStrain> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BacStrain::getDelFlag, 0);
        queryWrapper.eq(BacStrain::getRptGroup, params.getRptGroup());
        //queryWrapper.eq(BacStrain::getSampleda, params.getSampleda());
        /*Timestamp startTime = Timestamp.valueOf(params.getSampleda() + " 00:00:00");
        Timestamp endTime = Timestamp.valueOf(params.getSampleda() + " 23:59:59");*/
        Timestamp timestamp = new Timestamp(params.getSampleda().getTime());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String formattedDate = sdf.format(timestamp);
        Timestamp startTime = Timestamp.valueOf(formattedDate + " 00:00:00");
        Timestamp endTime = Timestamp.valueOf(formattedDate + " 23:59:59");
        queryWrapper.ge(BacStrain::getSampleda, startTime);
        queryWrapper.le(BacStrain::getSampleda, endTime);
        queryWrapper.eq(BacStrain::getSampleNo, params.getSampleNo());

        List<BacStrain> strainList = bacStrainMapper.selectList(queryWrapper);
        return Result.succ(1, "查询成功", strainList);

    }

    /*删除菌株*/
    @Override
    public Result deleteBacStrain(BacStrain bacStrain) {
        /*先删除君主记录*/
        int i = bacStrainMapper.deleteById(bacStrain.getId());
        if (i > 0 && ToolsUtils.isNotEmpty(bacStrain.getStrainCode())) {

            /*再删除培养观察记录*/
            LambdaQueryWrapper<BacObserResult> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(BacObserResult::getDelFlag, 0);
            queryWrapper.eq(BacObserResult::getSampleNo, bacStrain.getSampleNo());
            queryWrapper.eq(BacObserResult::getRptGroup, bacStrain.getRptGroup());
            if (ToolsUtils.isNotEmpty(bacStrain.getSampleda())) {
                Timestamp timestamp = new Timestamp(bacStrain.getSampleda().getTime());
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String formattedDate = sdf.format(timestamp);
                Timestamp startTime = Timestamp.valueOf(formattedDate + " 00:00:00");
                Timestamp endTime = Timestamp.valueOf(formattedDate + " 23:59:59");
                queryWrapper.ge(BacObserResult::getSampleda, startTime);
                queryWrapper.le(BacObserResult::getSampleda, endTime);
            }
            queryWrapper.eq(ToolsUtils.isNotEmpty(bacStrain.getTestFlowNo()), BacObserResult::getTestFlowNo, bacStrain.getTestFlowNo());
            queryWrapper.eq(ToolsUtils.isNotEmpty(bacStrain.getTestNodeNo()), BacObserResult::getTestNodeNo, bacStrain.getTestNodeNo());
            queryWrapper.and(p -> p.eq(BacObserResult::getIsolateNum, bacStrain.getStrainCode()).or().eq(BacObserResult::getStrainCount, bacStrain.getStrainCode()));

            bacObserResultMapper.delete(queryWrapper);

            /*再删除染色记录*/
            LambdaQueryWrapper<BacStainingResult> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(BacStainingResult::getDelFlag, 0);
            queryWrapper1.eq(BacStainingResult::getSampleNo, bacStrain.getSampleNo());
            queryWrapper1.eq(BacStainingResult::getRptGroup, bacStrain.getRptGroup());
            if (ToolsUtils.isNotEmpty(bacStrain.getSampleda())) {
                Timestamp timestamp = new Timestamp(bacStrain.getSampleda().getTime());
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String formattedDate = sdf.format(timestamp);
                Timestamp startTime = Timestamp.valueOf(formattedDate + " 00:00:00");
                Timestamp endTime = Timestamp.valueOf(formattedDate + " 23:59:59");
                queryWrapper1.ge(BacStainingResult::getSampleda, startTime);
                queryWrapper1.le(BacStainingResult::getSampleda, endTime);
            }
            queryWrapper1.eq(ToolsUtils.isNotEmpty(bacStrain.getTestFlowNo()), BacStainingResult::getTestFlowNo, bacStrain.getTestFlowNo());
            queryWrapper1.eq(ToolsUtils.isNotEmpty(bacStrain.getTestNodeNo()), BacStainingResult::getTestNodeNo, bacStrain.getTestNodeNo());
            queryWrapper1.and(p -> p.eq(BacStainingResult::getIsolateNum, bacStrain.getStrainCode()).or().eq(BacStainingResult::getStrainNo, bacStrain.getStrainCode()));

            bacStainingResultMapper.delete(queryWrapper1);
        }
        return i > 0 ? Result.succ(1, "删除成功", bacStrain) : Result.fail("删除失败");
    }
}
